package com.example.demo.generator.codegen.util;

import org.apache.commons.lang3.StringUtils;

/**
 * String case conversion helpers extracted from SpringBootCodeGenerator.
 */
public final class StringCaseUtils {

    private StringCaseUtils() {
    }

    public static String upperCaseFirst(String str) {
        if (StringUtils.isBlank(str)) {
            return str;
        }
        return str.substring(0, 1).toUpperCase() + str.substring(1);
    }

    public static String lowerCaseFirst(String str) {
        if (StringUtils.isBlank(str)) {
            return "";
        }
        return str.substring(0, 1).toLowerCase() + str.substring(1);
    }

    public static String underlineToCamelCase(String underscoreName) {
        if (StringUtils.isBlank(underscoreName)) {
            return underscoreName;
        }
        StringBuilder result = new StringBuilder();
        boolean upper = false;
        for (int i = 0; i < underscoreName.length(); i++) {
            char ch = underscoreName.charAt(i);
            if (ch == '_') {
                upper = true;
                continue;
            }
            if (upper) {
                result.append(Character.toUpperCase(ch));
                upper = false;
            } else {
                result.append(ch);
            }
        }
        return result.toString();
    }

    public static String toUnderline(String str, boolean upperCase) {
        if (StringUtils.isBlank(str)) {
            return str;
        }
        StringBuilder result = new StringBuilder();
        boolean prevUnderscore = false;
        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);
            if (ch == '_') {
                prevUnderscore = true;
            } else if (ch == '-') {
                ch = '_';
                prevUnderscore = true;
            } else if (Character.isUpperCase(ch)) {
                if (!prevUnderscore && i > 0) {
                    result.append('_');
                }
                prevUnderscore = false;
            } else {
                prevUnderscore = false;
            }
            result.append(upperCase ? Character.toUpperCase(ch) : Character.toLowerCase(ch));
        }
        return result.toString();
    }

    public static String toLowerCamel(String str) {
        if (StringUtils.isBlank(str)) {
            return str;
        }
        StringBuilder result = new StringBuilder();
        char prev = '\0';
        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);
            if (ch == '-' || ch == '—' || ch == '_') {
                prev = '_';
                continue;
            }
            char current = ch;
            if (prev == '_') {
                current = Character.toUpperCase(ch);
            } else if (Character.isUpperCase(prev)) {
                current = Character.toLowerCase(ch);
            }
            prev = ch;
            result.append(current);
        }
        return lowerCaseFirst(result.toString());
    }

    public static boolean isNotNull(String str) {
        return StringUtils.isNotEmpty(str);
    }
}
